/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.core.arrange.strategy;

import com.arisgo.cloud.attendance.clazz.core.arrange.MatchStore;
import com.arisgo.cloud.attendance.clazz.core.arrange.MatchStrategy;
import com.arisgo.cloud.attendance.clazz.entity.StaffClass;
import com.arisgo.cloud.attendance.clazz.mapper.StaffClassMapper;
import com.arisgo.cloud.attendance.clazz.vo.ClassInfoVo;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 循环匹配 cycleMatch
 *   > 时间以一天为准
 *   > 第一员工匹配第一班，第二员工匹配第二班...
 *   > 班排完后，再从第一个开始循环
 *   > 员工排完后结束
 *   > 员工人数少于班数，则班有空余
 *
 * @author xu keqi
 * @since 2025-03-29
 */
@Slf4j
@Component
@AllArgsConstructor
public class CycleMatch implements MatchStrategy, InitializingBean {

    public static final String KEY = "cycleMatch";

    private final MatchStore matchStore;

    private final StaffClassMapper staffClassMapper;

    @Override
    public void match(List<ClassInfoVo> infos, List<Long> staffIds) {
        if (CollectionUtils.isEmpty(infos) || CollectionUtils.isEmpty(staffIds)) {
            log.warn("排班策略 => 参数为空！");
            return;
        }
        log.info("排班策略 => 员工与班循环匹配");
        Long tenantId = infos.get(0).getTenantId();
        Map<LocalDate, List<ClassInfoVo>> infoGroup = infos.stream().collect(Collectors.groupingBy(ClassInfoVo::getWorkDate));

        List<StaffClass> staffClasses = Lists.newArrayList();
        // 一天一天排班
        for (List<ClassInfoVo> classInfos : infoGroup.values()) {
            // 以人为主，班次循环使用
            int i = 0, j = 0;
            while (i < staffIds.size()) {
                StaffClass staffClass = new StaffClass();
                staffClass.setTenantId(tenantId);
                staffClass.setStaffId(staffIds.get(i));
                staffClass.setClassId(classInfos.get(j).getId());
                staffClasses.add(staffClass);
                // 获取下一个班
                j++;
                if (j >= classInfos.size()) {
                    j = 0;
                }
                i++;
            }
        }
        if (!CollectionUtils.isEmpty(staffClasses)) {
            log.info("排班策略 => 保存关联关系！");
            staffClassMapper.insertBatch(staffClasses);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        matchStore.add(KEY, this);
    }
}
